/**
 *
 */
package com.dcivision.framework.taglib.html;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.taglib.logic.PermissionTag;

/**
 * Button with Permission
 *
 * @author TC.tang
 * @creationdate 2007-4-27
 */
public class PermissionButtonTag extends StandardButtonTag {

  private static final Log log = LogFactory.getLog(PermissionButtonTag.class);

  private String objectType;

  private Integer objectID;

  private String functionCode;

  private String right;

  private String styleClass;

  private String disabledStyleClass;

  public int doStartTag() throws JspException {
    try {
      if (getPermission()) {
        setDisable(false);
      } else {
        setDisable(true);
      }
    } catch (Exception ex) {
      log.error("Exception caught when starting tag.", ex);
    }

    return super.doStartTag();
  }

  private boolean getPermission() throws IOException, JspException {
    PermissionTag permission = new PermissionTag();
    BodyContent bc = pageContext.pushBody();
    bc.write("\ntrue\nfalse");
    permission.setBodyContent(bc);
    permission.setPageContext(pageContext);
    permission.setObjectType(getObjectType());
    permission.setObjectID(getObjectID());
    permission.setFunctionCode(getFunctionCode());
    permission.setRight(getRight());
    boolean result = getPermission(permission);
    pageContext.popBody();
    return result;
  }

  private boolean getPermission(PermissionTag permission) throws IOException, JspException {
    Writer imgTagOut = new StringWriter();
    BodyContent bc = pageContext.pushBody();
    permission.doStartTag();
    permission.doEndTag();
    permission.release();
    pageContext.popBody();
    bc.writeOut(imgTagOut);
    if(imgTagOut!=null&&imgTagOut.toString().startsWith("true")){
      return true;
    }
    return false;
  }

  public int doEndTag() {
    return EVAL_PAGE;
  }

  public void release() {
    super.release();
    objectType = null;
    objectID = null;
    functionCode = null;
    right = null;
  }

  public String getFunctionCode() {
    return functionCode;
  }

  public void setFunctionCode(String functionCode) {
    this.functionCode = functionCode;
  }

  public Integer getObjectID() {
    return objectID;
  }

  public void setObjectID(Integer objectID) {
    this.objectID = objectID;
  }

  public String getObjectType() {
    return objectType;
  }

  public void setObjectType(String objectType) {
    this.objectType = objectType;
  }

  public String getRight() {
    return right;
  }

  public void setRight(String right) {
    this.right = right;
  }

  public String getStyleClass() {
    return styleClass;
  }

  public void setStyleClass(String styleClass) {
    this.styleClass = styleClass;
  }

  public String getDisabledStyleClass() {
    return disabledStyleClass;
  }

  public void setDisabledStyleClass(String disabledStyleClass) {
    this.disabledStyleClass = disabledStyleClass;
  }

  protected String getButtonEnabledMainClass() {
    if(StringUtils.isEmpty(getStyleClass())) {
      return super.getButtonEnabledMainClass();
    }

    return getStyleClass();
  }

  protected String getButtonDisabledMainClass(){
    if(StringUtils.isEmpty(getDisabledStyleClass())){
      return super.getButtonDisabledMainClass();
    }
    return getDisabledStyleClass();
  }




}
